package com.fancy.learncenter.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.adapter.ConditionAdapter;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.DataCategoryDataBean;
import com.fancy.learncenter.bean.EducationDataBean;
import com.fancy.learncenter.bean.JsonBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.GetJsonDataUtil;
import com.google.gson.Gson;
import com.superservice.lya.R;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyy on 2018/2/24.
 * Explain activity as
 */

public class PopupWindowMaterrial extends BasedPopupWindow {

    View popopView;
    View viewParent;

    RecyclerView recyclerView;

    Activity mContext;

    public PopupWindowMaterrial(Activity mContext, View viewParent) {
        super(mContext);
        this.mContext = mContext;
        this.viewParent = viewParent;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.pupopwinodw_data_root, null);
        initPopupWionw();
    }

    ImageView delete;

    private void initPopupWionw() {
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        this.setOutsideTouchable(true);
        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        delete = popopView.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        recyclerView = (RecyclerView) popopView.findViewById(R.id.recycleView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new CommonRecycleViewAdapter<DataCategoryDataBean>(mContext, R.layout.pupopwinodw_data, new ArrayList<DataCategoryDataBean>()) {
            @Override
            public void bindView(CustomViewHold customViewHold, final DataCategoryDataBean item, int position) {


                TextView title = customViewHold.getView(R.id.title);
                title.setText(item.getCategory_name());


                if (item.getList() != null) {
                    RecyclerView recyclerView = customViewHold.getView(R.id.recycleView);
                    GridLayoutManager linearLayoutManager = new GridLayoutManager(mContext, 4);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(linearLayoutManager);

                    recyclerView.setAdapter(new CommonRecycleViewAdapter<DataCategoryDataBean.ListBean>(mContext, R.layout.pupopwinodw_data_item, (ArrayList<DataCategoryDataBean.ListBean>) item.getList()) {
                        @Override
                        public void bindView(CustomViewHold customViewHold, final DataCategoryDataBean.ListBean items, int position) {

                            SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
                            simpleDraweeView.setImageURI(items.getCategory_icon());


                            TextView title = customViewHold.getView(R.id.title);
                            title.setText(items.getCategory_name());


                            customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    onCallBack.onCallBack(items);
                                    dismiss();
                                }
                            });

                        }
                    });

                }


            }
        };
        recyclerView.setAdapter(adapter);

        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        getDatas();

        this.update();

        this.setContentView(popopView);


    }


    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        showAtLocation(viewParent, Gravity.CENTER, 0, 0);

    }

    private OnCallBack onCallBack;

    public interface OnCallBack {
        void onCallBack(DataCategoryDataBean.ListBean items);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public OnCallBack getOnCallBack() {

        return onCallBack;
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    CommonRecycleViewAdapter<DataCategoryDataBean> adapter;

    private void getDatas() {
        Map map = new HashMap();
        HttpMehtod.getInstance().dataCategory(map, new IdeaObserver<BaseDataBean<List<DataCategoryDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<DataCategoryDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    adapter.notifyDataSetChanged((ArrayList<DataCategoryDataBean>) listBaseDataBean.getData());
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

}
